/* 
 * Promise 封装 api
 */

import {HTTP_REQUEST_URL} from '@/config.js'

/* 
 * uni.getSystemInfo
 */

const getSystemInfo = () => {
	return new Promise((resolve, reject) => {
		uni.getSystemInfo({
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/* 
 * uni.getLocation   获取定位
 */

const uniGetLocation = () => {
	return new Promise((resolve, reject) => {
		uni.getLocation({
			type: 'gcj02',
			isHighAccuracy: true,
			altitude:true,
			highAccuracyExpireTime:5000,
			accuracy:50,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/* 
 *  上传文件
 */
const uniUploadFile = (options) => { 
	// console.log( uni.getStorageSync('cookiekey'))
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: HTTP_REQUEST_URL+options.url,
			filePath: options.filePath,
			name: options.name,
			header: {
				"Content-Type": "multipart/form-data",
				'token': uni.getStorageSync('token')
			},
			formData: options.formData,
			success(res) {
				resolve(res) 
			},
			fail() {
				reject()
			}
		})
	})
}

 
/*
 * opts:请求参数 url, 
 * 网络资源保存临时地址
 */
const downloadFile = (opts) => {
	return new Promise((resolve, reject) => {
		uni.downloadFile({
			url: opts.url,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

/*
 * opts:请求参数 url,data,datatype,method, 
 * canvas绘制导出
 */
const canvasToTempFilePath = (opts) => {
	return new Promise((resolve, reject) => {
		uni.canvasToTempFilePath({
			x: '' || opts.x,
			y: '' || opts.y,
			width: '' || opts.width,
			height: '' || opts.height,
			destWidth: '' || opts.destWidth,
			destHeight: '' || opts.destHeight,
			quality: '' || opts.destHeight,
			fileType: '' || opts.fileType,
			canvasId: opts.canvasId,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

const uniShowToast = (opts) => {
	let title = opts.title,
		icon = '',
		image = '',
		duration = 2000;
	if (opts.icon && (opts.icon == 'success' || opts.icon == 'loading')) {
		image = '';
		icon = opts.icon;
	} else {
		image = opts.image;
		icon = 'none';
	}
	if (opts.duration) {
		duration = opts.duration;
	}
	return new Promise((resolve, reject) => {
		uni.showToast({
			title: title,
			icon: icon,
			image: image,
			mask: true,
			duration: duration,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}

const uniShowLoading = (title) => {
	// let title=opts.title; 	 
	return new Promise((resolve, reject) => {
		uni.showLoading({
			title: title,
			mask: true,
			success: (res) => {
				resolve(res);
			},
			fail: (err) => {
				reject(err)
			}
		})
	})
}



export {
	getSystemInfo,
	downloadFile,
	canvasToTempFilePath,
	uniShowToast,
	uniShowLoading,
	uniUploadFile
}
